import { handleActions as createReducer } from 'redux-actions'
import { load_todo_success, add_todo_success, remove_todo_success, modify_todo_name_success, modify_todo_eidt_success, clear_todo_completed_success, motify_todo_success, motify_filter_todo } from '../actions/todo.actions'
import { fromJS, setIn, getIn, mergeDeep, updateIn } from 'immutable' 

/**s
 * 接收到指令之后对数据做响应的处理
 * 
 */

const initialState = fromJS({
  todos: [],
  filter: 'all'
})

const load_todo_action = (state, action) => {
  // 第一个参数 : 我们想要操作的数据是什么
  // 第二个参数: 我们当前想要操作的那一个属性
  // 第三个参数: 用于表示更新后的值
  return setIn(state, ['todos'], action.payload)
}

const add_todo_action = (state, action) => {
  // return { ...state,todos: [...state.todos, action.payload] }

  //mergeDeep 合并数据
  // 第二个参数是要合并的属性及被合并的值
  return mergeDeep(state, {todos: [action.payload], }) 
}

const remove_todo_success_action = (state, action) => {
  return {...state, todos: action.payload }
}

const modify_todo_action = (state, action) => {
  let index =  getIn(state, ['todos']).findIndex(todo => todo.id === action.payload.id)
  // let todos = JSON.parse(JSON.stringify(state.todos))
  // todos[index].isCompleted = action.payload.status
  // console.log(todos)
  // return {...state, todos}
  
  // 第二个参数是要更新的属性
  // 第三个参数是我们要更新成的目标值
  return updateIn(state, ['todos', index], () => action.payload )

}

const motify_filter_todo_action = (state, action) => {
  return setIn(state, ['filter'], action.payload)
  // return {
  //   ...state,
  //   filter: action.payload
  // }
}

const clear_todo_completed_success_action = (state, action) => {
  let todoList = getIn(state, ['todos']).filter(todo => todo.isCompleted === 0)
  return setIn(state, ['todos'], todoList)
  // return {
  //   todos: todoList,
  //   filter: state.filter
  // }
}

const modify_todo_eidt_success_action = (state, action) => {
  // 利用id,找到需要被操作的配置项,设置isEditing状态,显示具体的样式
  console.log(action)
  let todos = JSON.parse(JSON.stringify(state.todos))
  let index = todos.findIndex(todo => todo.id === action.payload.id)
  todos[index].isEditing = action.payload.status
  console.log(action)
  return { ...state, todos }
}

const modify_todo_name_success_action = (state, action) => {
  console.log(action, '=====================')
  let todos = JSON.parse(JSON.stringify(state.todos))
  let index = todos.findIndex(todo => todo.id === action.payload.id)
  todos[index].taskName = action.payload.taskName
  todos[index].isEditing = 0
  console.log(action)
  return { ...state, todos }
}

export default createReducer({
  [load_todo_success]: (state, action) => load_todo_action(state, action),
  [add_todo_success]: (state, action) => add_todo_action(state, action),
  [remove_todo_success]: (state, action) => remove_todo_success_action(state, action),
  [motify_todo_success]: (state, action) => modify_todo_action(state, action),
  [motify_filter_todo]: (state, action) => motify_filter_todo_action(state, action),
  [clear_todo_completed_success]: (state, action) => clear_todo_completed_success_action(state, action),
  [modify_todo_eidt_success]: (state, action) => modify_todo_eidt_success_action(state, action),
  [modify_todo_name_success]: (state, action) => modify_todo_name_success_action(state, action)
}, initialState)

